package com.dems.core.util;

/**
 * @Author houzhanshan
 * @Description
 * @Date 2017/11/21 下午11:08
 */


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

public class PropertyConverter {
    private static final Logger logger = LoggerFactory.getLogger(PropertyConverter.class);
    private static final String SVUID = "serialVersionUID";
    private static final String GET_METHOD = "get";

    public PropertyConverter() {
    }

    public static Map<String, Object> object2Map(Object obj) {
        return object2Map(obj, (List) null, false);
    }

    public static Map<String, Object> object2Map(Object obj, List<String> excludeFields) {
        return object2Map(obj, excludeFields, false);
    }

    public static MultiValueMap<String, String> object2StringMap(Object obj, List<String> excludeFields) {
        Map<String, Object> from = object2Map(obj, excludeFields, true);
        if (from == null) {
            return null;
        } else {
            MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
            Iterator var4 = from.entrySet().iterator();

            while (var4.hasNext()) {
                Map.Entry entry = (Map.Entry) var4.next();
                map.add((String) entry.getKey(), (String) entry.getValue());
            }

            return map;
        }
    }

    public static Map<String, Object> object2Map(Object obj, List<String> excludeFields, boolean trasfer2String) {
        if (obj == null) {
            return null;
        } else {
            StringBuilder getName = null;
            Map map = new LinkedHashMap();

            for (Class clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
                Field[] fields = clazz.getDeclaredFields();

                for (int i = 0; i < fields.length; ++i) {
                    String name = fields[i].getName();
                    if (!buildExclude(excludeFields).contains(name)) {
                        getName = (new StringBuilder("get")).append(name.substring(0, 1).toUpperCase()).append(name.substring(1, name.length()));

                        try {
                            Method getMethod = clazz.getDeclaredMethod(getName.toString());
                            if (getMethod != null) {
                                Object value = getMethod.invoke(obj);
                                if (trasfer2String && value != null) {
                                    if (value instanceof Date) {
                                        map.put(name, dateToStringFormat((Date) value));
                                    } else {
                                        map.put(name, value.toString());
                                    }
                                } else {
                                    map.put(name, value);
                                }
                            }
                        } catch (NoSuchMethodException var11) {
                            ;
                        } catch (Exception var12) {
                            logger.warn("转换属性异常", var12);
                        }
                    }
                }
            }

            return map;
        }
    }

    private static List<String> buildExclude(List<String> srcList) {
        List toList = new ArrayList();
        toList.add("serialVersionUID");
        if (srcList != null && srcList.size() > 0) {
            toList.addAll(srcList);
        }

        return toList;
    }

    public  static  String dateToStringFormat(Date date){
        if(date==null){
            return null;
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }
}

